/*
 * TailFile module for Netbeans.
 *
 * Copyright (c) 2007, Rogue
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * For queries or bugs, you may contact me via e-mail: roguexz [at] gmail [dot] com
 */
package rogue.netbeans.module.tail;

import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.util.actions.CallableSystemAction;

import org.openide.windows.WindowManager;

import java.awt.Component;
import java.awt.EventQueue;

import java.io.File;

import java.util.HashMap;
import java.util.Map;

import javax.swing.JFileChooser;

/**
 * The main entry point for this module
 *
 * @author Rogue
 */
public final class TailAction extends CallableSystemAction
    implements FileMonitorListener {
    
    // Reference to all the files that we are currently monitoring.
    private Map monitors = new HashMap();

    public void performAction() {
        EventQueue.invokeLater(new Runnable() {
                public void run() {
                    invokeAction();
                }
            });
    }

    public String getName() {
        return NbBundle.getMessage(TailAction.class, "CTL_TailAction");
    }

    protected void initialize() {
        super.initialize();
        putValue("noIconInMenu", Boolean.TRUE);
    }

    public HelpCtx getHelpCtx() {
        return HelpCtx.DEFAULT_HELP;
    }

    protected boolean asynchronous() {
        return true;
    }

    private void invokeAction() {
        // Launch the file chooser for the user to select the file to tail.
        JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fc.setMultiSelectionEnabled(false);

        Component mainWindow = WindowManager.getDefault().getMainWindow();
        int retValue = fc.showOpenDialog(mainWindow);

        if (retValue == JFileChooser.APPROVE_OPTION) {
            File f = fc.getSelectedFile();

            if (monitors.containsKey(f)) {
                ((FileMonitor) monitors.get(f)).ensureVisible();

                return;
            }

            FileMonitor fm = new FileMonitor(f);
            monitors.put(f, fm);
            fm.addFileMonitorListener(this);
            new Thread(fm).start();
        }
    }

    public void taskTerminated(FileMonitor monitor) {
        monitors.remove(monitor.getFile());
    }
}
